{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f36bfb4-6088-4d7d-a7c7-63a2c08c3cce",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "\n",
    "from langchain.output_parsers import PydanticOutputParser\n",
    "from langchain.prompts import ChatPromptTemplate\n",
    "from langchain.schema import HumanMessage\n",
    "from langchain_core.pydantic_v1 import BaseModel, Field\n",
    "from langchain_openai import ChatOpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fe7289eb-7682-4921-861e-96bc79ec0886",
   "metadata": {},
   "outputs": [],
   "source": [
    "class BookInfo(BaseModel):\n",
    "    book_name: str = Field(description=\"书籍的名字\", example=\"百年孤独\")\n",
    "    author_name: str = Field(description=\"书籍的作者\", example=\"加西亚·马尔克斯\")\n",
    "    genres: List[str] = Field(description=\"书籍的体裁\", example=[\"小说\", \"文学\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "67577918-9c24-4048-9be2-318ba6a41ded",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_parser = PydanticOutputParser(pydantic_object=BookInfo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "353d313f-ab0d-4ea5-a412-dbde60a89e06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The output should be formatted as a JSON instance that conforms to the JSON schema below.\n",
      "\n",
      "As an example, for the schema {\"properties\": {\"foo\": {\"title\": \"Foo\", \"description\": \"a list of strings\", \"type\": \"array\", \"items\": {\"type\": \"string\"}}}, \"required\": [\"foo\"]}\n",
      "the object {\"foo\": [\"bar\", \"baz\"]} is a well-formatted instance of the schema. The object {\"properties\": {\"foo\": [\"bar\", \"baz\"]}} is not well-formatted.\n",
      "\n",
      "Here is the output schema:\n",
      "```\n",
      "{\"properties\": {\"book_name\": {\"title\": \"Book Name\", \"description\": \"\\u4e66\\u7c4d\\u7684\\u540d\\u5b57\", \"example\": \"\\u767e\\u5e74\\u5b64\\u72ec\", \"type\": \"string\"}, \"author_name\": {\"title\": \"Author Name\", \"description\": \"\\u4e66\\u7c4d\\u7684\\u4f5c\\u8005\", \"example\": \"\\u52a0\\u897f\\u4e9a\\u00b7\\u9a6c\\u5c14\\u514b\\u65af\", \"type\": \"string\"}, \"genres\": {\"title\": \"Genres\", \"description\": \"\\u4e66\\u7c4d\\u7684\\u4f53\\u88c1\", \"example\": [\"\\u5c0f\\u8bf4\", \"\\u6587\\u5b66\"], \"type\": \"array\", \"items\": {\"type\": \"string\"}}}, \"required\": [\"book_name\", \"author_name\", \"genres\"]}\n",
      "```\n"
     ]
    }
   ],
   "source": [
    "print(output_parser.get_format_instructions())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7a6810c9-1452-4107-9743-96fa4050173c",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"{parser_instructions} 你输出的结果请使用中文。\"),\n",
    "    (\"human\", \"请你帮我从书籍概述中，提取书名、作者，以及书籍的体裁。书籍概述会被三个#符号包围。\\n###{book_introduction}###\")\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "537ba30b-17ad-4d25-a365-1474a1330275",
   "metadata": {},
   "outputs": [],
   "source": [
    "book_introduction = \"\"\"《明朝那些事儿》，作者是当年明月。2006年3月在天涯社区首次发表，2009年3月21日连载完毕，边写作边集结成书出版发行，一共7本。\n",
    "《明朝那些事儿》主要讲述的是从1344年到1644年这三百年间关于明朝的一些故事。以史料为基础，以年代和具体人物为主线，并加入了小说的笔法，语言幽默风趣。对明朝十六帝和其他王公权贵和小人物的命运进行全景展示，尤其对官场政治、战争、帝王心术着墨最多，并加入对当时政治经济制度、人伦道德的演义。\n",
    "它以一种网络语言向读者娓娓道出三百多年关于明朝的历史故事、人物。其中原本在历史中陌生、模糊的历史人物在书中一个个变得鲜活起来。《明朝那些事儿》为读者解读历史中的另一面，让历史变成一部活生生的生活故事。\n",
    "\"\"\"\n",
    "final_prompt = prompt.invoke({\"book_introduction\": book_introduction, \n",
    "                              \"parser_instructions\": output_parser.get_format_instructions()})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "33a3d876-fd17-43e7-860e-075f7915a6ae",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"book_name\": \"明朝那些事儿\",\n",
      "    \"author_name\": \"当年明月\",\n",
      "    \"genres\": [\"小说\", \"历史\"]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "model = ChatOpenAI(model=\"gpt-3.5-turbo\")\n",
    "response = model.invoke(final_prompt)\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0b1ccbf8-e1a0-4f8f-8f2b-8c8eb312803c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BookInfo(book_name='明朝那些事儿', author_name='当年明月', genres=['小说', '历史'])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = output_parser.invoke(response)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a26f6b54-8c3a-40c7-8325-9f202a6ca2fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'明朝那些事儿'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.book_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b56cddcd-7a15-4dca-bdbd-6c46a69c003d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小说', '历史']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.genres"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2a99bc8-96cc-405b-bd21-f78b75b4e561",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
